package com.aba.cjms.teacher;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

import com.aba.cjms.pojo.ConnectionDatabaseForMySQLUseJdbc;

public class TeacherDao {
	
	
	//设置一个全局变量用来存储成功登录的账号
    public static String account;

    static  Connection connection;
	   static PreparedStatement pstmt;
	   static ResultSet rset;


    //教师登录输入账号和密码 与数据库比较 成功 登录成功返回 登录成功的语句    否则返回登录失败并提升重新输入登录账号和密码循环
    public static void login(){
        System.out.println("请输入教师账号");
        try{
            Scanner scanner = new Scanner(System.in);
            String account = scanner.next();
            System.out.println("请输入密码");
            String password = scanner.next();
            connection = ConnectionDatabaseForMySQLUseJdbc.getConnection();

            String sql = "select * from teacher where taccount = ? and tpassword = ?";
            PreparedStatement pstmt = connection.prepareStatement(sql);
            pstmt.setString(1,account);
            pstmt.setString(2,password);
            ResultSet rset = pstmt.executeQuery();
            if(rset.next()){
                System.out.println("教师登录成功");
                TeacherDao.account = account;
                System.out.println("欢迎您"+TeacherDao.account);
                System.out.println("请选择功能");
                TeacherDao.showMenu();

            }
               else
                    {
                        System.out.println("登录失败");
                        TeacherDao.login();

                    }

               pstmt.close();
               connection.close();
               return;


                }
            catch(Exception e){
        e.printStackTrace();
    }
            }

        //显示菜单1录入学生成绩2查看学生成绩3查看学生信息4查看教师信息5查看教师自己信息6退出系统
        public static void showMenu() throws Exception{
        System.out.println("1录入学生成绩");
        System.out.println("2查看学生成绩");
        System.out.println("3查看学生信息");

        System.out.println("4退出系统");
        Scanner sc = new Scanner(System.in);

        int choice = sc.nextInt();
        switch(choice){
        case 1:
            inputScore();
            System.out.print("-----");
            break;
            case 2:
                queryScore();
                break;
           case 3:
                    queryStudent();
                    break;
            case 4:
                        
                         exit();                	
                               
    }
        }

            //1录入学生成绩 先输入学生的账号  通过学生账号查询学生的老师的账号若与登录的账号一致 则可以录入学生成绩 否则提示无法录入并退出到显示菜单
            public static void inputScore() throws Exception{
            	
            			System.out.println("请输入学生的账号");
            			Scanner sc = new Scanner(System.in);
            			String account = sc.next();
            			connection = ConnectionDatabaseForMySQLUseJdbc.getConnection();
            			String sql = "select * from student where saccount = ?";
            			PreparedStatement pstmt = connection.prepareStatement(sql);
            			pstmt.setString(1,account);
            			ResultSet rset = pstmt.executeQuery();
            			while(rset.next()){
            				String taccount = rset.getString("saccount");
            				if(taccount.equals(TeacherDao.account)){
            					System.out.println("请输入学生的成绩");
            					Scanner sc1 = new Scanner(System.in);
            					int score = sc1.nextInt();
            					String sql1 = "update student set score = ? where saccount = ?";
            					PreparedStatement pstmt1 = connection.prepareStatement(sql1);
            					pstmt1.setInt(1,score);
            					pstmt1.setString(2,account);
            					pstmt1.executeUpdate();
            					System.out.println("录入成功");
            					
            					System.out.println("是否继续录入");
            					System.out.println("1继续录入 2返回菜单");
            					Scanner sc11 = new Scanner(System.in);
            					int choice = sc11.nextInt();
            					switch(choice){
            					case 1:
            						inputScore();
            						break;
            					case 2:
            						TeacherDao.showMenu();
            					}
            				}
            				else{
            					System.out.println("无法录入");
            					TeacherDao.showMenu();
            				}
            				}	
            		}
            		
            	



            //2查看学生成绩
            public static void queryScore(){
        try{
            System.out.println("请输入学生的账号");
            Scanner sc = new Scanner(System.in);
            String account = sc.next();
            connection = ConnectionDatabaseForMySQLUseJdbc.getConnection();
            String sql = "select * from student where saccount = ?";
            PreparedStatement pstmt = connection.prepareStatement(sql);
            pstmt.setString(1,account);
            ResultSet rset = pstmt.executeQuery();
            while(rset.next()){

                String saccount = rset.getString("saccount");
                if(saccount.equals(TeacherDao.account)){
                    System.out.println("学生成绩为："+rset.getString("score"));
                    TeacherDao.showMenu();
                    System.out.println("是否继续查看");
                    System.out.println("1继续查看 2返回菜单");
                    Scanner sc1 = new Scanner(System.in);
                    int choice = sc1.nextInt();
                    switch(choice){
                        case 1:
                            queryScore();
                            break;
                            case 2:
                                TeacherDao.showMenu();
                    }

                }
                else{
                    System.out.println("无法查看");
                    TeacherDao.showMenu();
                }
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
            }


            //查看学生信息
            public static void queryStudent(){
        try{
            System.out.println("请输入学生的账号");
            Scanner sc = new Scanner(System.in);
            String account = sc.next();
            connection = ConnectionDatabaseForMySQLUseJdbc.getConnection();
            String sql = "select * from student where saccount = ?";
            PreparedStatement pstmt = connection.prepareStatement(sql);
            pstmt.setString(1,account);
            ResultSet rset = pstmt.executeQuery();
            while(rset.next()){
                String taccount = rset.getString("saccount");
                if(taccount.equals(TeacherDao.account)){
                    System.out.println("学生姓名为："+rset.getString("sname"));
                    System.out.println("学生学号为："+rset.getString("saccount"));
                    System.out.println("学生成绩为："+rset.getString("score"));
                    System.out.println("是否继续查看");
                    System.out.println("1继续查看 2返回菜单");
                    Scanner sc1 = new Scanner(System.in);
                    int choice = sc1.nextInt();
                    switch(choice){
                        case 1:
                            queryStudent();
                            break;
                            case 2:
                                TeacherDao.showMenu();

                    }
                }
                else{
                    System.out.println("该学生你无法查看");
                    TeacherDao.showMenu();
                }
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
            
            public static void exit() throws Exception{
                
                System.out.println("是否退出系统");
                System.out.println("1.是 2.否");
                Scanner sc = new Scanner(System.in);
                int choice = sc.nextInt();
                switch(choice){
                    case 1:
                        System.out.println("退出系统");
                        System.out.println("欢迎下次再来");
                        TeacherDao.login();
                        break;
                        case 2:
                            TeacherDao.showMenu();
                            break;
                }
                }
            
            
            
            
            
            
            
//
//            public static void main(String[] args) {
//            	 TeacherDao.login();
//			}
//            
            
            
            
            
        }
	
	
	
	


